package com.zy.面向对象.继承

object Test {
  def main(args: Array[String]): Unit = {
    val student: Student = new Student("张三", 12, "三年级二班")
    println(student)
  }
}

class Person {
  var name: String = _
  var age: Int = _

  println("【父类】的主构造器被调用!")

  def this(name: String, age: Int) {
    this()
    println("【父类】的辅助构造器被调用")
    this.name = name
    this.age = age
  }

  override def toString = s"Person($name, $age)"
}
// 这样会调用父类的辅助构造器
//class Student(name: String, age: Int) extends Person(name: String, age: Int) {
// 这样不会调用父类的辅助构造器
class Student(name: String, age: Int) extends Person {
  var classes: String = _
  println("【子类】的主构造器被调用!")

  def this(name: String, age: Int, classes: String) {
    this(name, age)
    println("【子类】的辅助构造器被调用")
    this.classes = classes
  }

  override def toString = s"Student($name,$age,$classes)"
}
